<?php
namespace TuxCocktail\TuxDb\Mysql\Cocktail;


use TuxCocktail\TuxDb\Mysql\TuxDbMysqlAbstract;
use TuxCocktail\TuxDb\TuxSurveAddinsInterface;
use TuxCocktail\TuxDb\Mysql\Cocktail\Model\SurveAttrTable;
use TuxCocktail\TuxDb\Mysql\Cocktail\Model\SurveAttrTypeTable;

class TuxDbMysqlSurveAddins extends TuxDbMysqlAbstract implements TuxSurveAddinsInterface {

	/**
	 * 
	 * @var SurveAttrTable
	 */
	public $surveAttrTable;
	
	/**
	 * 
	 * @return \TuxCocktail\TuxDb\Mysql\Cocktail\Model\SurveAttrTable
	 */
	private function getSurveAttrTable() {
		if (!$this->surveAttrTable) {
			$this->surveAttrTable = new SurveAttrTable($this->tuxDb->getInstance());
		}
		return $this->surveAttrTable;
	}
	
	/**
	 * 
	 * @var SurveAttrTypeTable
	 */
	private $surveAttrTypeTable;
	
	
	/**
	 * 
	 * @return \TuxCocktail\TuxDb\Mysql\Cocktail\Model\SurveAttrTypeTable
	 */
	function getSurveAttrTypeTable() {
		if (!$this->surveAttrTypeTable) {
			$this->surveAttrTypeTable = new SurveAttrTypeTable($this->tuxDb->getInstance());
		}
		return $this->surveAttrTypeTable;
	}
	
	
	function addSurveAttributes($cocktailId,$surveAttributes) {
		assert($cocktailId != null);
		$surveAttrTypeTable = $this->getSurveAttrTypeTable();
		$surveAttrTable = $this->getSurveAttrTable();
		foreach ($surveAttributes as $attribute) {
			$select = $surveAttrTypeTable->newSelect();
			$surveAttrTypeTable->getSelect($select,array("surve_attr_type_id"))->where(array('surve_attr_type_name'=>$attribute));
			$resultSet = $surveAttrTypeTable->selectWIth($select);
			if ($resultSet->count() == 0) {
				continue;
			}
			$row = $resultSet->current();
			$surveAttrTypeId = $row->surve_attr_type_id;
			$surveAttrTable->addSurveAttr($cocktailId,$surveAttrTypeId);
		}
	
	}
	
	
	public function getSurveAttrTypes() {
		$surveAttrTypeTable = $this->getSurveAttrTypeTable();
		$select = $surveAttrTypeTable->newSelect();
		$surveAttrTypeTable->getSelect($select,array('surve_attr_type_id','global_image_id','surve_attr_type_name'));
		$resultSet = $surveAttrTypeTable->selectWith($select);
		if ($resultSet->count() >0) {
			return $resultSet->toArray();
		}
		return null;
	}
	
	public function getCocktailSurveAttributes($cocktailId) {
		$surveAttr=$this->getSurveAttrTable();
		$select = $surveAttr->newSelect();
		$surveAttr->getSelect($select,array())
		->join('surve_attr_type','surve_attr_type.surve_attr_type_id = surve_attr.surve_attr_type_id',array('surve_attr_type_form_name'))
		->where(array('surve_attr.cocktail_id'=>$cocktailId));
		$resultSet = $surveAttr->selectWith($select);
		if ($resultSet->count() == 0) {
			return null;
		} else {
			$result=array();
			foreach ($resultSet as $row) {
				array_push($result,$row->surve_attr_type_form_name);
			}
			return $result;
		}
	}
	
	public function updateAddinName($id,$newValue) {
		$surveAttrTypeTable = $this->getSurveAttrTypeTable();
		$surveAttrTypeTable->update(array('surve_attr_type_name'=>$newValue),array('surve_attr_type_id'=>$id));
	}
	
	public function getSurveAttrId($surveAttrName) {
		$surveAttrTypeTable = $this->getSurveAttrTypeTable();
		$select = $surveAttrTypeTable->newSelect();
		$surveAttrTypeTable->getSelect($select,array('surve_attr_type_id'))->where(array('surve_attr_type_name'=>$surveAttrName));
		$resultSet = $surveAttrTypeTable->selectWith($select);
		$resultArray = $resultSet->toArray();
		if (sizeof($resultArray) != 1) {
			return null;
		}
		$surveAttrTypeId=$resultArray[0]['surve_attr_type_id'];
		return $surveAttrTypeId;		
	}
	
	public function getSurveAttrName($surveAttrId) {
		$surveAttrTypeTable = $this->getSurveAttrTypeTable();
		$select = $surveAttrTypeTable->newSelect();
		$surveAttrTypeTable->getSelect($select,array('surve_attr_type_name'))->where(array('surve_attr_type_id'=>$surveAttrId));
		$resultSet = $surveAttrTypeTable->selectWith($select);
		$resultArray = $resultSet->toArray();
		if (sizeof($resultArray) != 1) {
			return null;
		}
		$surveAttrTypeName=$resultArray[0]['surve_attr_type_name'];
		return $surveAttrTypeName;
		
	}
	
}
